<?php
/**
 * 文章分类
 * */
namespace app\admin\controller;
use app\admin\controller;


class Category extends AdminBase
{
    public function _initialize()
    {
        parent::_initialize();

        // 载入模型
        $this->model = Model('category');
    }
    public function index()
    {
        $ser['pid'] = input('pid/d');
        // 查询数据
        $fiels['list'] = $this->model->tree($ser['pid']);

        $fiels['opt'] = $this->model->treeOpt($ser['pid']);

        $this->assign('fiels',$fiels);
        return $this->fetch();
    }

    // 添加内容
    public function add(){
        $pid = input('pid/d');

       // echo $pid;
        //$fiels['tree']= $this->model->tree($pid);
        $fiels['tree']= $this->model->treeOpt($pid);
        //print_r($fiels);

        
        $this->assign('fiels',$fiels);


        return $this->fetch();
    }
    public function add_ok(){
        $input = input('post.');

        // 数据组合
        $data['pid']             = $input['pid'];
        $data['name']          = $input['name'];
        $data['sort']            = $input['sort'];
        $data['extend']         = $input['extend'];
        $data['desc']           = $input['desc'];
        $data['add_time']     = time();

        // 插入数据
        $res = $this->model->categoryInsert($data);

        $this->is_success($res);
    }

    // 修改内容
    public function edit(){
        $id = input('id/d','','intval');

        // 单条数据详情
        $fiels =  $this->model->info($id);
        // 分类数 opt格式
        $fiels['treeOpt'] = $this->model->treeOpt($fiels['pid']);

        $this->assign('fiels',$fiels);
        return $this->fetch();
    }
    public function edit_ok(){
        $input = input('post.');

        // 组合数据
        $data['pid']                = $input['pid'];
        $data['name']            = $input['name'];
        $data['sort']              = $input['sort'];
        $data['extend']          = $input['extend'];
        $data['desc']             = $input['desc'];
        //更新数据
        $res = $this->model->categoryUpdate($input['id'],$data);

        $this->is_success($res);
    }

    // 删除数据
    public function delete(){
        $id = input('id/d');
        $res = $this->model->categoryDelete($id);

        $this->is_success($res);

    }

    // 回收站
    public function ashcan(){

    }

}
